<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">

			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="部门名称"></el-input>
				</el-form-item>

				<!--
				上级部门查询
					parentDepts :数据源
					item:变量,部门对象
					key value :选择了之后 赋值filters.parentId
					label:给用户看的
				-->
				<el-select v-model="filters.parentId" clearable placeholder="上级部门">
					<el-option
							v-for="item in parentDepts"
							:key="item.id"
							:label="item.name"
							:value="item.id">
					</el-option>
				</el-select>

				<!-- 经理查询 -->
				<el-select v-model="filters.managerId" clearable placeholder="经理">
					<el-option
							v-for="item in managers"
							:key="item.id"
							:label="item.username"
							:value="item.id">
					</el-option>
				</el-select>
				<el-form-item>
					<el-button type="primary" v-on:click="getdept(1)">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>

		</el-col>

		<!--列表-->
		<!--		:data :绑定的数据-->
		<!--		v-loading: 页面css样式等东西加载之后加载加载数据-->
		<!--		prop:对应的是:data绑定的对象的属性-->
		<!--		label :描述-->
		<el-table :data="dept" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="部门名称" width="120" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
				<template slot-scope="scope">
					<el-button type="text" size="small" @click="suppstop(scope.$index, scope.row)"
							   :type="scope.row.state === 1?'primary':'danger'"
					>
						{{scope.row.state===1?"启用":"禁用"}}
					</el-button>
				</template>
			</el-table-column>
			<el-table-column prop="sn" label="部门编号" width="100" sortable>
			</el-table-column>

			<el-table-column prop="manager.username" label="部门经理" width="100" sortable>
			</el-table-column>

			<el-table-column prop="parent.name" label="上级部门" width="100" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<!--			current-change:自带的事件,我当前页发生改变时,就会触发此事件-->
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="操作" :visible.sync="editFormVisible"  :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="部门名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="部门编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="部门状态" prop="state">
					<el-switch
							style="display: block"
							v-model="state"
							active-color="#13ce66"
							inactive-color="#ff4949"
							active-text="启用"
							inactive-text="禁用">
					</el-switch>
				</el-form-item>
				<el-form-item label="部门经理" prop="">
					<el-select v-model="editForm.managerId" clearable placeholder="部门经理">
						<el-option
								v-for="item in employees"
								:key="item.id"
								:label="item.username"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>

				<el-form-item  label="上级部门" prop="">
					<!-- v-model获取选择的值
                     :props="{ checkStrictly: true ,value:'id',label:'name'}"
                                                    传递的值          给人看的
                     -->
					<el-cascader
							v-model="editForm.parentIds"
							:options="treeList"
							:props="{ checkStrictly: true ,value:'id',label:'name'}"
							clearable></el-cascader>
				</el-form-item>

			</el-form>



			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>


	</section>
</template>

<script>

	//import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	//import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: '',
					parentId : null,
					managerId : null,
				},
				treeList : [],
				employees : [],
				parentDepts : [],
				managers : [],
				dept: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				state : true ,
				//编辑界面数据
				editForm: {
					id:'',
					name: '',
					sn: '',
					parentIds : []
				},

			}
		},
		methods: {
			//性别显示转换
			//row 当前行row = user  column当前列
			formatSex: function (row, column) {
				return row.state == 1 ? '正常' : row.state == 0 ? '禁用' : '未知';
			},
			handleCurrentChange(val) {
				//var 就是当前页的数据
				this.page = val;
				this.getdept();
			},
			//获取部门列表
			getdept(num) {

				if (num == 1){
					this.page = 1
				}
				//封装的数据
				let para = {
					//page 就是当前页
					page: this.page,
					//这个是高级查询的条件
					keyword: this.filters.name,
					parentId : this.filters.parentId,
					managerId : this.filters.managerId
				};
				this.listLoading = true;
				//NProgress.start();
				this.$http.post("/dept/findPage",para).then((res) => {
					console.log(res);
					//
					this.total = res.data.total;
					//
					this.dept = res.data.list;
					//关闭进度条
					this.listLoading = false;
					//NProgress.done();
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					// let para = { id: row.id };
					this.$http.delete("/dept/"+row.id).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getdept();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				//显示编辑页面的模态框
				this.editFormVisible = true;
				//Object.assign 是拷贝对象 BeanUtil.copyof()
				this.editForm = row;
				// row.state 1 0  !1:false !!1:ture  1:1
				//this.state = row.state?true:false
				//优雅方式
				this.state = !!row.state

				//1.获取到这一行数据的dirPath字段
				let dirPath = row.dirPath;

				//2.安装/分隔
				let paths = dirPath.split("/")
				console.log(paths)
				//3.去头去尾
				paths = paths.slice(1,paths.length-1);
				//4.把paths转成数字数组  map映射成什么类型 Number
				paths = paths.map(Number)

				//5.赋值给组件的v-model的值
				this.editForm.parentIds=paths
				console.log("当前修改的数据是:"+JSON.stringify(this.editForm))



			},
			//显示新增界面
			handleAdd:function(){
				//显示编辑页面的模态框
				this.editFormVisible = true;

				this.editForm = {}
			},
			//提交
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					console.log(valid)
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							//这里面是获取数据的
							this.editLoading = true;
							//拷贝对象
							this.editForm.state = this.state ? 1 : 0

							let para = Object.assign({}, this.editForm);
							this.$http.post("/dept/saveOrupdate",para).then((res) => {
								console.log(res)
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: res.data.success?'提交成功':res.data.message,
									type: res.data.success?'success':'error'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getdept();
							});
						});
					}
				});
			},

			//复选勾选框的方法
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					this.$http.post("/dept/deleteBatch" , para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getdept();
					});
				}).catch(() => {

				});
			},
			findParentDepts () {
				this.$http.post("/dept/findParentDepts" ).then( res =>{
					console.log(res)
					this.parentDepts = res.data.data
				})
			},

			findmanagers () {
				this.$http.post("/emp/findManagers" ).then( res =>{
					console.log(res)
					this.managers = res.data.data
				})
			},

			findEmployees() {
				this.$http.get("/emp/findAll" ).then( res =>{
					console.log(res)
					console.log(res.data.data)
					this.employees = res.data.data
				})
			},

			deptTreeList(){
				this.$http.get("/dept/treeList").then(res=>{

					console.log(res)
					console.log(res.data.data)
					this.treeList = res.data.data

				})
			},

		},
		//就是页面已加载完成就会去触发这个mounted
		mounted() {
			//分页查询
			this.getdept();
			//查询上级部门
			this.findParentDepts();
			//查询经理们
			this.findmanagers();
			//发送请求获取所有的员工
			this.findEmployees();
			//获取后台分好的部门级联数据
			this.deptTreeList();

		}
	}

</script>

<style scoped>

</style>
